package qcmPlus.actions;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.ParameterAware;
import org.apache.struts2.interceptor.SessionAware;

import qcmPlus.beans.Question;
import qcmPlus.beans.Questionnaire;
import qcmPlus.beans.Reponse;
import qcmPlus.dao.QuestionDao;
import qcmPlus.dao.ReponseDAO;

import com.opensymphony.xwork2.ActionSupport;

public class GestionQuestionsAction extends ActionSupport implements
		ParameterAware, SessionAware {

	private static final long serialVersionUID = 1L;

	private Question question = new Question();
	private List<Question> questions = new ArrayList<Question>();
	private QuestionDao questionDao = new QuestionDao();
	private Questionnaire questionnaire = new Questionnaire();
	private ReponseDAO reponseDao = new ReponseDAO();
	private List<Reponse> listeReponses = new ArrayList<Reponse>();

	private String intituleQuestion;

	private int idQuestionnaire;

	private String intituleRep1;
	private String intituleRep2;
	private String intituleRep3;
	private String intituleRep4;
	private String intituleRep5;

	private boolean rep1EstBonne;
	private boolean rep2EstBonne;
	private boolean rep3EstBonne;
	private boolean rep4EstBonne;
	private boolean rep5EstBonne;

	private Reponse rep1 = new Reponse();
	private Reponse rep2 = new Reponse();
	private Reponse rep3 = new Reponse();
	private Reponse rep4 = new Reponse();
	private Reponse rep5 = new Reponse();

	protected int next = 0;

	private Reponse[] tabReps = { rep1, rep2, rep3, rep4, rep5 };

	private Map<String, Object> session;

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	private Map<String, String[]> parameters;

	public Map<String, String[]> getParameters() {
		return parameters;
	}

	public void setParameters(Map<String, String[]> parameters) {
		this.parameters = parameters;
	}

	public String getParameterValue(String param) {
		if (getParameters().get(param) == null)
			return "1";
		return ((String[]) getParameters().get(param))[0];
	}

	public String demarrer() {
		int idQuest = Integer.parseInt(getParameterValue("questId"));

		// questionnaire = (Questionnaire) session.get("questSelected");
		// idQuestionnaire = questionnaire.getId();

		questions = questionDao.liste(idQuest);
		session.put("listequestions", questions);
		return SUCCESS;

	}

	public String demarrer2() {
		int idQuest = Integer.parseInt(getParameterValue("questId"));

		// questionnaire = (Questionnaire) session.get("questSelected");
		// idQuestionnaire = questionnaire.getId();

		questions = questionDao.liste(idQuest);
		session.put("listequestions", questions);
		return SUCCESS;

	}

	public int getNext() {
		return next;
	}

	public void setNext(int next) {
		this.next = next;
	}

	private int id;

	@Override
	public String execute() {
		questionnaire = (Questionnaire) session.get("questSelected");
		idQuestionnaire = questionnaire.getId();
		questions = questionDao.liste(idQuestionnaire);
		return SUCCESS;

	}

	public String creationQuestion() {
		questionnaire = (Questionnaire) session.get("questSelected");
		idQuestionnaire = questionnaire.getId();
		return SUCCESS;

	}

	public String edit() {
		String tmp[] = new String[5];
		boolean reptmp[] = new boolean[5];
		question = questionDao.getQuestion(getId());
		session.put("questionSelected", question);
		listeReponses = reponseDao.liste(question.getId());
		session.put("lesReponses", listeReponses);

		for (int i = 0; i < listeReponses.size(); i++) {
			if (listeReponses.get(i).getIntitule().equals(null)) {
				tmp[i] = null;
				reptmp[i] = false;
			} else {
				tmp[i] = listeReponses.get(i).getIntitule();
				reptmp[i] = listeReponses.get(i).isEstBonneReponse();
			}
		}

		intituleRep1 = tmp[0];
		intituleRep2 = tmp[1];
		intituleRep3 = tmp[2];
		intituleRep4 = tmp[3];
		intituleRep5 = tmp[4];

		rep1EstBonne = reptmp[0];
		rep2EstBonne = reptmp[1];
		rep3EstBonne = reptmp[2];
		rep4EstBonne = reptmp[3];
		rep5EstBonne = reptmp[4];

		return SUCCESS;
	}

	public String modification() {

		question = (Question) session.get("questionSelected");
		question.setIntituleQuestion(intituleQuestion);

		listeReponses = question.getListeReponses();

		// pour la boucle for
		String[] tabinitule = { intituleRep1, intituleRep2, intituleRep3,
				intituleRep4, intituleRep5 };
		boolean[] tabool = { rep1EstBonne, rep2EstBonne, rep3EstBonne,
				rep4EstBonne, rep5EstBonne };

		for (int j = 0; j < 5; j++) {
			if (j < listeReponses.size()) {
				listeReponses.get(j).setIntitule(tabinitule[j]);
				listeReponses.get(j).setEstBonneReponse(tabool[j]);
			} else if (!tabinitule[j].isEmpty()) {
				tabReps[j].setIntitule(tabinitule[j]);
				tabReps[j].setEstBonneReponse(tabool[j]);
				listeReponses.add(tabReps[j]);
			}

		}

		question.setListeReponses(listeReponses);

		String result = questionDao.modification(question);
		if (result.equals("ok"))
			return SUCCESS;
		else
			return ERROR;

	}

	public String insertion() {
		questionnaire = (Questionnaire) session.get("questSelected");
		idQuestionnaire = questionnaire.getId();

		rep1.setIntitule(intituleRep1);
		rep1.setEstBonneReponse(rep1EstBonne);
		rep2.setIntitule(intituleRep2);
		rep2.setEstBonneReponse(rep2EstBonne);
		rep3.setIntitule(intituleRep3);
		rep3.setEstBonneReponse(rep3EstBonne);
		rep4.setIntitule(intituleRep4);
		rep4.setEstBonneReponse(rep4EstBonne);
		rep5.setIntitule(intituleRep5);
		rep5.setEstBonneReponse(rep5EstBonne);

		for (int i = 0; i < 5; i++) {
			if (!tabReps[i].getIntitule().equals(null))
				listeReponses.add(tabReps[i]);
		}

		String result = questionDao.insertion(questionnaire,
				getIntituleQuestion(), getListeReponses());

		if (result.equals("ok"))
			return SUCCESS;
		else
			return ERROR;

	}

	
	public String suppression() {
		question = (Question) session.get("questionSelected");
		String result = questionDao.suppression(question);
		if (result.equals("ok"))
			return SUCCESS;
		else
			return ERROR;

	}

	/**
	 * getters et setters
	 * 
	 * @return l'intitule de la question
	 */

	public String getIntituleQuestion() {
		return intituleQuestion;
	}

	public void setIntituleQuestion(String intitule) {
		this.intituleQuestion = intitule;
	}

	public int getIdQuestionnaire() {
		return idQuestionnaire;
	}

	public void setIdQuestionnaire(int idQuestionnaire) {
		this.idQuestionnaire = idQuestionnaire;
	}

	public List<Question> getQuestions() {
		return questions;
	}

	public void setQuestions(List<Question> questions) {
		this.questions = questions;
	}

	public QuestionDao getQuestionDao() {
		return questionDao;
	}

	public void setQuestionDao(QuestionDao questionDao) {
		this.questionDao = questionDao;
	}

	public String getIntituleRep1() {
		return intituleRep1;
	}

	public void setIntituleRep1(String intituleRep1) {
		this.intituleRep1 = intituleRep1;
	}

	public String getIntituleRep2() {
		return intituleRep2;
	}

	public void setIntituleRep2(String intituleRep2) {
		this.intituleRep2 = intituleRep2;
	}

	public String getIntituleRep3() {
		return intituleRep3;
	}

	public void setIntituleRep3(String intituleRep3) {
		this.intituleRep3 = intituleRep3;
	}

	public String getIntituleRep4() {
		return intituleRep4;
	}

	public void setIntituleRep4(String intituleRep4) {
		this.intituleRep4 = intituleRep4;
	}

	public String getIntituleRep5() {
		return intituleRep5;
	}

	public void setIntituleRep5(String intituleRep5) {
		this.intituleRep5 = intituleRep5;
	}

	public boolean isRep1EstBonne() {
		return rep1EstBonne;
	}

	public void setRep1EstBonne(boolean rep1EstBonne) {
		this.rep1EstBonne = rep1EstBonne;
	}

	public boolean isRep2EstBonne() {
		return rep2EstBonne;
	}

	public void setRep2EstBonne(boolean rep2EstBonne) {
		this.rep2EstBonne = rep2EstBonne;
	}

	public boolean isRep3EstBonne() {
		return rep3EstBonne;
	}

	public void setRep3EstBonne(boolean rep3EstBonne) {
		this.rep3EstBonne = rep3EstBonne;
	}

	public boolean isRep4EstBonne() {
		return rep4EstBonne;
	}

	public void setRep4EstBonne(boolean rep4EstBonne) {
		this.rep4EstBonne = rep4EstBonne;
	}

	public boolean isRep5EstBonne() {
		return rep5EstBonne;
	}

	public void setRep5EstBonne(boolean rep5EstBonne) {
		this.rep5EstBonne = rep5EstBonne;
	}

	public Question getQuestion() {
		return question;
	}

	public void setQuestion(Question question) {
		this.question = question;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public List<Reponse> getListeReponses() {
		return listeReponses;
	}

	public void setListeReponses(List<Reponse> listeReponses) {
		this.listeReponses = listeReponses;
	}

}
